var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
var common = require('../../../utils/common.js')
let chooseYear = null;
let chooseMonth = null;
let asign = 0;
let asign2 = 0;
let asign3 = 0;
let bbb = [];
let ccc = [];
const conf = {
  data: {
    hasEmptyGrid: false,
    showPicker: false,
    show: false,
    title:'',
    kemucolor: [],
    content: [],
    content2: [],
    orgid: "",
    userid: "",
    ti: [],
    times:'',
    num: 1,
    scrollHeight: 0,
    
  },
  onLoad(options) {
    
    let c = common.Getorgid();
    const date = new Date();
    const curYear = date.getFullYear();
    const curMonth = date.getMonth() + 1;
    const strDate = date.getDate();
    const weeksCh = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(curYear, curMonth);
    this.calculateDays(curYear, curMonth);
    var times = curYear + "-" + curMonth + "-" + strDate;
    var scrollHeight = scrollHeight;
    wx.getSystemInfo({
      success: function (res) {
        scrollHeight = res.windowHeight
      }
    });
    if (c.role == "学生") {
      this.setData({
        times:times,
        curYear,
        curMonth,
        weeksCh,
        strDate,
        orgid: c.orgid,
        userid: c.userid,
        scrollHeight: scrollHeight,
      });
    }
    else {
      this.setData({
        times: times,
        curYear,
        curMonth,
        weeksCh,
        strDate,
        orgid: c.orgid,
        userid: options.id,
        scrollHeight: scrollHeight,
      });
    }
    console.log(this.data.times)
    this.list();
    
    this.tapDayItem();
  },
  onShow(){
     asign = 0;
     asign2 = 0;
     asign3 = 0;
     bbb = [];
     ccc = [];
  },
  getdata: function (alength, data, res) {
    if (asign == alength) {
      // console.log(bbb)
    } else {
      //8.7.2获取该生今日作业的完成情况
      let params = { "orgid": data.orgid, "userid": data.userid, "searchField": "peopleid,examid", "searchValue": "" + data.userid + "," + res[asign].id + "" }
      businessLogic.GetTheStudentsToday(params, (resa) => {
        bbb.push(resa);
        // if (bbb.length == aaa.length) {
        for (var k = 0; k < res.length; k++) {
          res[k].aaaaa = bbb[k];
        }
         //console.log(res)
          this.setData({
            content: res
          });
        // }
        asign += 1;
        this.getdata(alength, data, res);
      })

    }

  },
  bindDownLoad: function () {
    if (this.data.lenpage > this.data.num) {
      var di = this.data.num;
      var di = di + 1;
      this.setData({
        num: di
      });
      this.list();
    } else {
      common.showToast("到底了", "loading");
    }
  },
  //8.7.1获取今日作业列表
  list: function () {
    //common.showLoading("加载中", "loading");
    var aaa = []; 

    // 第二步 传参
    let params = { "page": this.data.num, "num": 15, "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "ishomework,hwdate", "searchValue": "0,"+this.data.times+"" }
    //第三步 调用接口
    businessLogic.GetTheJobListToday(params, (res) => {
      // console.log("8.7.1", res)
      //第五步 把获取的值，传入data
     /*  var page = res.page
      var res = res.infos; */
      for (let a = 0; a < res.length; a++) {
        // console.log(res[a].title);
        var x = "";
        var kemucolor1 = ""
        switch (res[a].subject_dictionary_title) {
          case "语文":
            x = "yuwen"
            break;
          case "数学":
            x = "shuxue"
            break;
          case "英语":
            x = "english"
            break;
          case "物理":
            x = "wuli"
            break;
          case "化学":
            x = "huaxue"
            break;
          case "政治":
            x = "zhengzhi"
            break;

          default:
            x = "zhengzhi";
        }
        res[a].color = [];
        res[a].color.push(x);
      }
      /* var hhh = []
      for (let s = 0; s < res.length; s++) {
        hhh.push(res[s]);
      } */
      this.setData({
        content: res,
        //lenpage: page.pages

      });
      let a = 0;
      for (let a = 0; a < this.data.content.length; a++) {
        aaa[aaa.length] = this.data.content[a];
        if (a == (this.data.content.length - 1)) {
          this.getdata(aaa.length, this.data, res)
        }
      };
    });

  },


  getdata2: function (alength, data, res) {
    if (asign2 == alength) {
       asign2 = 0;
      //console.log(ccc)
    } else {
      //8.7.2获取该生今日作业的完成情况
      let params = { "orgid": data.orgid, "userid": data.userid, "searchField": "peopleid,examid", "searchValue": "" + data.userid + "," + res[asign2].id + "" }
      businessLogic.GetTheStudentsToday(params, (resa) => {
        // console.log(resa);
        ccc.push(resa);
        // if (bbb.length == aaa.length) {
        for (var k = 0; k < res.length; k++) {
          res[k].aaaaa = ccc[k];
        }
        // console.log(res)
        this.setData({
          content: res
        });
        // }
        asign2 += 1;
        this.getdata2(alength, data, res);
      })

    }

  },

  list2: function () {
    var aaa = [];

    // 第二步 传参
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "ishomework,hwdate,title", "searchValue": "0,"+this.data.mydata+"," + this.data.title + "" }
    //第三步 调用接口
    businessLogic.GetTheJobListToday(params, (res) => {
      // console.log("8.7.1", res)
      //第五步 把获取的值，传入data
      for (let a = 0; a < res.length; a++) {
        // console.log(res[a].title);
        var x = "";
        var kemucolor1 = ""
        switch (res[a].subject_dictionary_title) {
          case "语文":
            x = "yuwen"
            break;
          case "数学":
            x = "shuxue"
            break;
          case "英语":
            x = "english"
            break;
          case "物理":
            x = "wuli"
            break;
          case "化学":
            x = "huaxue"
            break;
          case "政治":
            x = "zhengzhi"
            break;

          default:
            x = "zhengzhi";
        }
        res[a].color = [];
        res[a].color.push(x);
      }
      this.setData({
        content: res

      });
      let a = 0;
      for (let a = 0; a < this.data.content.length; a++) {
        aaa[aaa.length] = this.data.content[a];
        if (a == (this.data.content.length - 1)) {
          this.getdata2(aaa.length, this.data, res)
        }
      };
    });

  },

  bindKeyInput: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  search2: function (event) {
    bbb = [];
    ccc = [];
    this.list2()
  }, 
 
  getdata3: function (alength, data, res) {
    if (asign3 == alength) {
      asign3 = 0;
      //console.log(ccc)
    } else {
      //8.7.2获取该生今日作业的完成情况
      let params = { "orgid": data.orgid, "userid": data.userid, "searchField": "peopleid,examid", "searchValue": "" + data.userid + "," + res[asign3].id + "" }
      businessLogic.GetTheStudentsToday(params, (resa) => {
        // console.log(resa);
        ccc.push(resa);
        // if (bbb.length == aaa.length) {
        for (var k = 0; k < res.length; k++) {
          res[k].aaaaa = ccc[k];
        }
        // console.log(res)
        this.setData({
          content: res
        });
        // }
        asign3 += 1;
        this.getdata3(alength, data, res);
      })

    }

  },

  list3: function () {
    var aaa = [];

    // 第二步 传参
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "ishomework,hwdate,title", "searchValue": "0,"+this.data.mydata+"," + this.data.title + "" }
    //第三步 调用接口
    businessLogic.GetTheJobListToday(params, (res) => {
      // console.log("8.7.1", res)
      //第五步 把获取的值，传入data
      for (let a = 0; a < res.length; a++) {
        // console.log(res[a].title);
        var x = "";
        var kemucolor1 = ""
        switch (res[a].subject_dictionary_title) {
          case "语文":
            x = "yuwen"
            break;
          case "数学":
            x = "shuxue"
            break;
          case "英语":
            x = "english"
            break;
          case "物理":
            x = "wuli"
            break;
          case "化学":
            x = "huaxue"
            break;
          case "政治":
            x = "zhengzhi"
            break;

          default:
            x = "zhengzhi";
        }
        res[a].color = [];
        res[a].color.push(x);
      }
      this.setData({
        content: res

      });
      let a = 0;
      for (let a = 0; a < this.data.content.length; a++) {
        aaa[aaa.length] = this.data.content[a];
        if (a == (this.data.content.length - 1)) {
          this.getdata2(aaa.length, this.data, res)
        }
      };
    });

  },


  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);
      }
      this.setData({
        hasEmptyGrid: true,
        empytGrids
      });
    } else {
      this.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
    }
  },
  calculateDays(year, month) {
    let days = [];

    const thisMonthDays = this.getThisMonthDays(year, month);

    for (let i = 1; i <= thisMonthDays; i++) {
      days.push({
        day: i,
        choosed: false
      });
    }

    this.setData({
      days
    });
  },
  handleCalendar(e) {
    const handle = e.currentTarget.dataset.handle;
    const curYear = this.data.curYear;
    const curMonth = this.data.curMonth;
    if (handle === 'prev') {
      let newMonth = curMonth - 1;
      let newYear = curYear;
      if (newMonth < 1) {
        newYear = curYear - 1;
        newMonth = 12;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        curYear: newYear,
        curMonth: newMonth
      });
    } else {
      let newMonth = curMonth + 1;
      let newYear = curYear;
      if (newMonth > 12) {
        newYear = curYear + 1;
        newMonth = 1;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        curYear: newYear,
        curMonth: newMonth
      });
    }
  },
  tapDayItem(e) {
    
    if(e==undefined){
      var myDate = new Date();
      var shijian = myDate.getDate();
      const idx = shijian - 1;
      const days = this.data.days;
      for (let index = 0; index < days.length; index++) {
        days[index].choosed = false;
      }

      days[idx].choosed = !days[idx].choosed;
      this.setData({
        days,
        mydata: this.data.curYear + "-" + this.data.curMonth + "-" + days[idx].day

      });
    }else{
      const idx = e.currentTarget.dataset.idx;
      const days = this.data.days;
      for (let index = 0; index < days.length; index++) {
        days[index].choosed = false;
      }

      days[idx].choosed = !days[idx].choosed;
      this.setData({
        days,
        mydata: this.data.curYear + "-" + this.data.curMonth + "-" + days[idx].day,
        show:false
      });
      console.log(this.data.curYear + "-" + this.data.curMonth + "-" + days[idx].day)
    
    }
   

    //8.7.1
   this.list3();
  },
  chooseYearAndMonth() {
    const curYear = this.data.curYear;
    const curMonth = this.data.curMonth;
    let pickerYear = [];
    let pickerMonth = [];
    for (let i = 1900; i <= 2100; i++) {
      pickerYear.push(i);
    }
    for (let i = 1; i <= 12; i++) {
      pickerMonth.push(i);
    }
    const idxYear = pickerYear.indexOf(curYear);
    const idxMonth = pickerMonth.indexOf(curMonth);
    this.setData({
      pickerValue: [idxYear, idxMonth],
      pickerYear,
      pickerMonth,
      showPicker: true,
    });
  },
  pickerChange(e) {
    const val = e.detail.value;
    chooseYear = this.data.pickerYear[val[0]];
    chooseMonth = this.data.pickerMonth[val[1]];
  },
  onShareAppMessage() {
    return {
      title: '小程序日历',
      desc: '还是新鲜的日历哟',
      path: 'pages/index/index'
    };
  },
  Calendar() {
    this.setData({
      show: !this.data.show,
    });
  },
  testDet: function (event) {
   if (event.currentTarget.dataset.pigai == undefined || event.currentTarget.dataset.pigai == '未批改'){
      wx.navigateTo({
        url: "../answeron/answeron?title=试卷详情&&examid=" + event.currentTarget.dataset.examid +"&&remarktype=0&&examtype=1&&parid=" + event.currentTarget.dataset.id
      });
    }
    else{
      // wx.navigateTo({
      //   url: "../alreadyCorret/alreadyCorret?parid=" + event.currentTarget.dataset.id
      // });
      //记得到时改回
     wx.navigateTo({
       url: "../answeron/answeron?title=试卷详情&&examid=" + event.currentTarget.dataset.examid + "&&remarktype=3&&examtype=1&&parid=" + event.currentTarget.dataset.id
     });
    } 
  //  console.log(this.data.content);
   
  },
};

Page(conf);